package diamondRingDao.impl;

import diamondRingDao.AdminDao;
import diamondRingEntity.Admin;
import diamondRingEntity.DiamondRingEntity;
import tool.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    /*
    后台登录
     */
    @Override
    public int Login(Admin admin) {
        ResultSet rs=null;
        int count=0;
        try {
            String sql="select count(*) from admin where adminName=? and adminPwd=?";
            Object [] params ={admin.getName(), admin.getPwd()};
            rs=super.executeQuery(sql,params);
            if (rs.next()){
                //拿到查询结果
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            System.err.println("数据访问层"+e);
        }
        return count;
    }

    /*
    后台修改密码
     */
    @Override
    public int UpdatePassword(Admin admin) {
        int count=0;
        try {
            String sql = "UPDATE admin SET adminPwd=? WHERE adminName=?";
            Object [] params ={admin.getPwd(),admin.getName()};
            count = super.executeUpdate(sql,params);
        } catch (Exception e) {
            System.err.println("数据访问层"+e);
        }
        return count;
    }

    /*
    用户查询
     */
    @Override
    public List<Admin> SelectAdmin() {
        ResultSet rs = null;
        Admin admin = null;
        List<Admin> list = new ArrayList<>();
        try {
            String sql = "SELECT id,adminName,adminSex,adminAge,adminPhone FROM admin";
            rs = this.executeQuery(sql, null);
            while (rs.next()) {
                admin = new Admin();
                admin.setID(rs.getString(1));
                admin.setName(rs.getString(2));
                admin.setSex(rs.getString(3));
                admin.setAge(rs.getString(4));
                admin.setPhone(rs.getString(5));
                list.add(admin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
    按用户名查询用户
     */
    @Override
    public List<Admin> SelectNameAdmin(String adminName) {
        ResultSet rs = null;
        Admin admin = null;
        List<Admin> list = new ArrayList<>();
        try {
            String sql = "SELECT id,adminName,adminSex,adminAge,adminPhone FROM admin where adminName like ?";
            Object [] param = {'%'+adminName+'%'};
            rs = this.executeQuery(sql, param);
            while (rs.next()) {
                admin = new Admin();
                admin.setID(rs.getString(1));
                admin.setName(rs.getString(2));
                admin.setSex(rs.getString(3));
                admin.setAge(rs.getString(4));
                admin.setPhone(rs.getString(5));
                list.add(admin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
    删除用户
     */
    @Override
    public int DeleteIdAdmin(String ID) {
        int count = 0 ;
        try {
            String sql = "DELETE FROM admin where id=?";
            Object [] param = {ID};
            count = super.executeUpdate(sql,param);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /*
    查看用户信息
     */
    @Override
    public List<Admin> SelectAdminInfoAdmin(String ID) {
        ResultSet rs = null;
        Admin admin = null;
        List<Admin> list = new ArrayList<>();
        try {
            String sql = "SELECT id,adminName,adminSex,adminBrithday,adminPhone,adminAdeessr FROM admin where id=?";
            Object [] param = {ID};
            rs = this.executeQuery(sql, param);
            while (rs.next()) {
                admin = new Admin();
                admin.setID(rs.getString(1));
                admin.setName(rs.getString(2));
                admin.setSex(rs.getString(3));
                admin.setBirthday(rs.getString(4));
                admin.setPhone(rs.getString(5));
                admin.setAdeer(rs.getString(6));
                list.add(admin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
    按用户ID修改用户信息
     */
    @Override
    public int UpdateAdminInfo(Admin admin,String id) {
        int count=0;
        try {
            String sql = "UPDATE admin SET adminName=?,adminSex=?,adminBrithday=?,adminPhone=?,adminAdeessr=? WHERE id=?";
            Object [] params ={admin.getName(),admin.getSex(),admin.getBirthday(),admin.getPhone(),admin.getAdeer(),id};
            count = super.executeUpdate(sql,params);
        } catch (Exception e) {
            System.err.println("数据访问层"+e);
        }
        return count;
    }

    /*
    添加用户
     */
    @Override
    public int InsertAdminInfo(Admin admin) {
        int count = 0;
        try {
            String sql = "insert INTO admin(adminName,adminPwd,adminSex,adminAge,adminPhone,adminBrithday,adminAdeessr)\n" +
                    "VALUES(?,?,?,?,?,?,?)";
            Object [] params ={admin.getName(),admin.getPwd(),admin.getSex(),admin.getAge(),admin.getPhone(),admin.getBirthday(),admin.getAdeer()};
            count = super.executeUpdate(sql,params);
        } catch (Exception e) {
            System.err.println("数据访问层"+e);
        }
        return count;
    }

    /*
    商品查询
     */
    @Override
    public List<DiamondRingEntity> getAll() {
        ResultSet rs=null;
        List<DiamondRingEntity>list=new ArrayList<>();
        try {
            String sql="SELECT id,goodspath,se.diamondseries,st.style,ty.type,diamondprice,di.sellnum,di.stock,di.goods\n" +
                    "FROM diamondmessage di \n" +
                    "INNER JOIN series se ON di.seriesid=se.seriesid\n" +
                    "INNER JOIN diamondtype ty ON di.typeid=ty.typeid\n" +
                    "INNER JOIN style st ON st.styleid=di.styleid";
            rs=super.executeQuery(sql,null);
            while (rs.next()){
                DiamondRingEntity diao=new DiamondRingEntity();
                diao.setId(rs.getInt(1));
                diao.setGoodsPath(rs.getString(2));
                diao.setSeries(rs.getString(3));
                diao.setStyle(rs.getString(4));
                diao.setType(rs.getString(5));
                diao.setDiamondPrice(rs.getInt(6));
                diao.setSellNum(rs.getInt(7));
                diao.setStock(rs.getInt(8));
                diao.setGoods(rs.getInt(9));
                list.add(diao);
            }
        } catch (SQLException e) {
            System.out.println("数据访问层出错"+e.getMessage());
        }
        return list;
    }

    /*
    按钻戒ID查询钻戒信息
     */
    @Override
    public List<DiamondRingEntity> LookDRinfo(int ID) {
        ResultSet rs = null;
        List<DiamondRingEntity> list = new ArrayList<>();
        try {
            String sql = "SELECT goodspath,id,se.diamondseries,st.style,te.texture,sh.shape,type.type,co.colour,carat,diamondprice FROM diamondmessage di\n" +
                    " INNER JOIN series se ON se.seriesid=di.seriesid \n" +
                    " INNER JOIN style st ON st.styleid=di.styleid\n" +
                    " INNER JOIN texture te ON te.textureid=di.textureid  \n" +
                    " INNER JOIN shape sh ON sh.shapeid=di.shapeid  \n" +
                    " INNER JOIN diamondtype type ON type.typeid=di.typeid  \n" +
                    " INNER JOIN colour co ON co.colourid=di.colorid  \n" +
                    "WHERE id =?";
            Object [] param = {ID};
            rs = super.executeQuery(sql, param);
            while (rs.next()) {
                DiamondRingEntity diao = new DiamondRingEntity();
                diao.setGoodsPath(rs.getString(1));
                diao.setId(rs.getInt(2));
                diao.setSeries(rs.getString(3));
                diao.setStyle(rs.getString(4));
                diao.setTexture(rs.getString(5));
                diao.setShape(rs.getString(6));
                diao.setType(rs.getString(7));
                diao.setColour(rs.getString(8));
                diao.setCarat(rs.getString(9));
                diao.setDiamondPrice(rs.getInt(10));
                list.add(diao);
            }
        } catch (SQLException e) {
            System.out.println("数据访问层出错" + e.getMessage());
        }
        return list;
    }
     /*
     根据系列模糊查询商品
      */
    @Override
    public List<DiamondRingEntity> vaugeFind(String vauge) {
        ResultSet rs=null;
        List<DiamondRingEntity>list=new ArrayList<>();
        try {
            String sql="SELECT id,di.goodspath,se.diamondseries,st.style,ty.type,di.diamondprice,di.sellnum,di.stock,di.goods FROM diamondmessage di \n" +
                    "INNER JOIN series se ON di.seriesid=se.seriesid\n" +
                    "INNER JOIN diamondtype ty ON di.typeid=ty.typeid \n" +
                    "INNER JOIN style st ON st.styleid=di.styleid \n" +
                    "where diamondseries like ?";
            Object[]pare={"%"+vauge+"%"};
            rs=super.executeQuery(sql,pare);
            while (rs.next()){
                DiamondRingEntity diao=new DiamondRingEntity();
                diao.setId(rs.getInt(1));
                diao.setGoodsPath(rs.getString(2));
                diao.setSeries(rs.getString(3));
                diao.setStyle(rs.getString(4));
                diao.setType(rs.getString(5));
                diao.setDiamondPrice(rs.getInt(6));
                diao.setSellNum(rs.getInt(7));
                diao.setStock(rs.getInt(8));
                diao.setGoods(rs.getInt(9));
                list.add(diao);
            }
        } catch (SQLException e) {
            System.out.println("数据访问层出错"+e.getMessage());
        }
        return list;
    }

    /*
    按商品ID修改商品信息
     */
    @Override
    public int UpdateGoodsInfo(DiamondRingEntity diamondRingEntity, int id) {
        int count = 0;
        try {
            String sql = "UPDATE diamondmessage SET goodspath=?,seriesid=?,styleid=?,textureid=?,shapeid=?,typeid=?,colorid=?,carat=?,diamondprice=?\n" +
                         "WHERE id =?";
            Object[] params = {diamondRingEntity.getGoodsPath(),diamondRingEntity.getSeries(),diamondRingEntity.getStyle(),diamondRingEntity.getTexture(),diamondRingEntity.getShape(),diamondRingEntity.getType(),diamondRingEntity.getColour(),diamondRingEntity.getCarat(),diamondRingEntity.getDiamondPrice(),id};
            count = super.executeUpdate(sql, params);
        } catch (Exception e) {
            System.err.println("数据访问层" + e);
        }
        return count;
    }

    /*
    按商品ID下架商品
     */
    @Override
    public int DelGoods(int id) {
        int count = 0;
        try {
        String sql = "update diamondmessage set goods=0 " +
                     "WHERE id =?";
        Object [] param = {id};
        count = super.executeUpdate(sql,param);
        } catch (Exception e) {
            System.err.println("数据访问层" + e);
        }
        return count;
    }

    /*
   按商品ID上架商品
    */
    @Override
    public int AddGoods(int id) {
        int count = 0;
        try {
            String sql = "update diamondmessage set goods=1 " +
                    "WHERE id =?";
            Object [] param = {id};
            count = super.executeUpdate(sql,param);
        } catch (Exception e) {
            System.err.println("数据访问层" + e);
        }
        return count;
    }

    /*
    新增商品
     */
    @Override
    public int Insert(DiamondRingEntity diamondRingEntity) {
        int count = 0;
        try {
            String sql = "INSERT  INTO diamondmessage(goodspath,goodspath2,seriesid,styleid,textureid,modeid,shapeid,objectid,typeid,colorid,carat,diamondprice,diamondnum,caratweight,stock,goods)\n" +
                         "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Object [] param = {diamondRingEntity.getGoodsPath(),diamondRingEntity.getGoodsPath2(),diamondRingEntity.getSeries(),diamondRingEntity.getStyle(),diamondRingEntity.getTexture(),diamondRingEntity.getMode(),diamondRingEntity.getShape(),diamondRingEntity.getObject(),diamondRingEntity.getType(),diamondRingEntity.getColour(),diamondRingEntity.getCarat(),diamondRingEntity.getDiamondPrice(),diamondRingEntity.getDiamondNum(),diamondRingEntity.getCaratWeight(),diamondRingEntity.getStock(),diamondRingEntity.getGoods()};
            count = super.executeUpdate(sql,param);
        } catch (Exception e) {
            System.err.println("数据访问层" + e);
        }
        return count;
    }
}
